<?php

class ajoutOeuvreValidation extends Controller {


	function index()
	{
		
		$this->load->database();
		$this->load->library('form_validation');
		$this->load->helper('form');
		$this->load->helper('url');
		$this->load->helper('path');
		
		//$config['upload_path'] ='C:\Program Files (x86)\EasyPHP-5.3.2i\www\codeIgnitor\CodeIgniter_1.7.2\CodeIgniter_1.7.2\image\media\\' ;
		
		$config['upload_path'] = set_realpath("./image/media/");
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '0';
		$config['max_width']  = '0';
		$config['max_height']  = '0';		
		$this->load->library('upload', $config);
		
		$data;	
		$image;
		$oeuvre;
		$okFile=true;
		
		$this->form_validation->set_rules('nom',  'Nom', 'required');		
		$this->form_validation->set_message('required', 'Vous n\'avez pas renseigner le champs %s ');
				
		if(isset($_FILES['fileImage']) && $_FILES['fileImage']['name']!=""){		
			$okFile=$this->upload->do_upload('fileImage');			
			$data['error'] = $this->upload->display_errors();	
		}
		else $image=($this->input->post('selectImage')) ;
		
		
		if ($this->form_validation->run() && $okFile ){ 
		if(isset($_FILES['fileImage']) && $_FILES['fileImage']['name']!=""){
			$data['upload_data'] =  $this->upload->data();
			$nomFile=$this->db->escape($data['upload_data']['file_name']);
			$sql = "INSERT INTO media (chemin,oeuvre,type,utilisateur)VALUES (".$nomFile.",NULL,'image','".$_COOKIE['login']."')";
			$this->db->query($sql);	
			$image=$this->db->insert_id();
			}
					
			$data["nom"]=($this->input->post('nom')) ;
			$this->db->set('nom',$this->input->post('nom'));		
			$this->db->set('presentation',$this->input->post('presentation'));
			$this->db->set('descriptif',$this->input->post('descriptif'));
			$this->db->set('auteur',$this->input->post('auteur'));
			$this->db->set('date_creation',$this->input->post('periode'));
			$this->db->set('lieu_creation',$this->input->post('pays'));
			$this->db->set('image_principale',$image);
			$this->db->set('utilisateur',$_COOKIE['login']);
			
			
			$this->db->insert('oeuvre'); 	
			$oeuvre=$this->db->insert_id();
		
		if ($this->input->post('selectExposition')!="") :
			foreach( ($this->input->post('selectExposition')) as $exposition):
				//$data["expositions"][]=$exposition;
				$this->db->set('exposition',$exposition);
				$this->db->set('oeuvre',$oeuvre);
				$this->db->insert('expose'); 
			endforeach;
		endif;
		
			$this->db->set('oeuvre',$oeuvre);
			$this->db->where('ID_Media', $image);
			$this->db->update('media'); 

		//$data['upload_data'] =  $this->upload->data();
		
			$this->load->view('ajoutOeuvreValidation',$data);
		}
		
		else {
				
			if (isset($_COOKIE['login'])){
			
		$query = $this->db->query("SELECT ID_Exposition, nom FROM exposition where utilisateur ='".$_COOKIE['login']."'");

		foreach ($query->result_array() as $row)
		{
			$data["expositions"][]=array($row['ID_Exposition'],$row['nom']);
		}
		
		$query = $this->db->query("SELECT ID_Media, chemin FROM media where type ='image' && oeuvre is NULL && utilisateur ='".$_COOKIE['login']."'");
		
		foreach ($query->result_array() as $row)
		{
			$data["images"][]=array($row["ID_Media"],$row['chemin']);
		}
		 }
		$this->load->view('ajoutOeuvre', $data);
		
		}
		
	}
	
	
	

}
?>
